import {
	baseUrl
} from './baseUrl.js';
import store from '@/store';
import Vue from 'vue';
import {
	getUserStatus
} from "@/core/api/user.js";
export default {
	// 全局配置
	common: {
		baseUrl, //请求地址
		header: {
			'Content-Type': 'application/x-www-form-urlencoded' //'application/x-www-form-urlencoded'
		},
		data: {},
		method: 'GET',
		dataType: 'json'
	},
	// 请求 返回promise
	async request(options = {}) {
		// 参数
		if (options.url.indexOf('/digital-mall/ignore/auth/api/general/login/status') == -1) {
			if(uni.getStorageSync('email')){
				let ress = await getUserStatus(uni.getStorageSync('email'));
				if(ress.loginStatus==1){
					Vue.prototype.$tool.showToast('当前账号已冻结');
					uni.removeStorageSync('token');
					uni.removeStorageSync('mobileUserId');
					uni.removeStorageSync('email');
					uni.removeStorageSync('mobileNumber');
					store.commit("config/set_isContinue", false);
					store.commit("config/set_isContinue2", false);
					setTimeout(res => {
						Vue.prototype.$tool.navTo('/pages/login/login', 3);
					}, 800)
					return;
				}
			}
		}
		options.url = this.common.baseUrl + options.url;
		options.header = options.header || this.common.header;
		options.data = options.data || this.common.data;
		options.method = options.method || this.common.method;
		options.dataType = options.dataType || this.common.dataType;
		let token;
		token = uni.getStorageSync('token');
		if (token) {
			options.header['X-Access-Token'] = token;
		}
		if (options.method == 'DELETE') {
			options.header["Content-Type"] = 'application/x-www-form-urlencoded';
		}
		if (options.data.isArray == 1) {
			// console.log(options.data.isArray);
			let obj = options.data;
			delete obj.isArray;
			options.data = obj;
			options.header["Content-Type"] = 'application/json'
		}
		// 请求
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				success: (result) => {
					// 请求成功
					let resData = result.data;
					if (resData.code == 200 || resData.code == 0) {
						resolve(resData);
					} else if (resData.code == 401) {
						Vue.prototype.$tool.showToast(Vue.prototype.$t('showToast').st006);
						uni.removeStorageSync('token');
						uni.removeStorageSync('mobileUserId');
						uni.removeStorageSync('email');
						uni.removeStorageSync('mobileNumber');
						store.commit("config/set_isContinue", false);
						store.commit("config/set_isContinue2", false);
						setTimeout(res => {
							Vue.prototype.$tool.navTo('/pages/login/login', 3);
						}, 500)
						reject(resData);
					} else if (resData.code == 403) {
						Vue.prototype.$tool.showToast(resData.message);
						reject(resData);
					} else if (resData.code == 404) {
						Vue.prototype.$tool.showToast(resData.message);
						reject(resData);
					} else {
						reject(resData);
						uni.showToast({
							title: resData.message,
							icon: 'none'
						})
						// Vue.prototype.$tool.showToast();
					}
				},
				fail: (error) => {
					Vue.prototype.$tool.showToast(Vue.prototype.$t('showToast').st007);
					reject(error)
				}
			});
		})
	},
	// get请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// delete请求
	del(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	},
	// options请求
	options(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'options'
		return this.request(options)
	},
	// put请求
	put(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'put'
		return this.request(options)
	},
	// head请求
	head(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'head'
		return this.request(options)
	},
	// trace请求
	trace(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'trace'
		return this.request(options)
	},
	// connect请求
	connect(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'connect'
		return this.request(options)
	},
}